<?php

namespace Mlym\CodeGeneration\InitGeneration;

use EasySwoole\Http\AbstractInterface\AbstractRouter;
use EasySwoole\HttpAnnotation\AnnotationController;
use EasySwoole\ORM\AbstractModel;
use Mlym\CodeGeneration\ClassGeneration\Config;
use Mlym\CodeGeneration\InitGeneration\Authorization\AuthorizationConfig;
use Mlym\CodeGeneration\InitGeneration\Authorization\AuthorizationGeneration;
use Mlym\CodeGeneration\InitGeneration\Controller\ControllerConfig;
use Mlym\CodeGeneration\InitGeneration\Controller\ControllerGeneration;
use Mlym\CodeGeneration\InitGeneration\Exception\ExceptionGeneration;
use Mlym\CodeGeneration\InitGeneration\Model\ModelConfig;
use Mlym\CodeGeneration\InitGeneration\Model\ModelGeneration;
use Mlym\CodeGeneration\InitGeneration\Router\RouterConfig;
use Mlym\CodeGeneration\InitGeneration\Router\RouterGeneration;
use Mlym\CodeGeneration\InitGeneration\UnitTest\UnitTestConfig;
use Mlym\CodeGeneration\InitGeneration\UnitTest\UnitTestGeneration;
use PHPUnit\Framework\TestCase;
use Exception;

/**
 * Class InitBaseGeneration
 * @package Mlym\CodeGeneration\InitBaseClass
 */
class InitBaseGeneration
{
    /**
     * 生成基础控制器基类
     * @param $modulePath
     * @return bool|int
     * @throws Exception
     */
    public function generationBaseController($modulePath)
    {
        $config = new ControllerConfig('Base', "App\\HttpController{$modulePath}");
        $config->setExtendClass(AnnotationController::class);
        $generation = new ControllerGeneration($config);
        return $generation->generate();
    }

    /**
     * 生成框架路由类文件
     * @return bool|string
     * @throws Exception
     */
    public function generationRouter()
    {
        $config = new RouterConfig('Router', "App\\HttpController");
        $config->setExtendClass(AbstractRouter::class);
        $generation = new RouterGeneration($config);
        return $generation->generate();
    }

    /**
     * Authorization权限校验类生成
     * @return bool|string
     * @throws Exception
     */
    public function generationAuthorization()
    {
        $config = new AuthorizationConfig('Authorization', "App\\Logic");
        $generation = new AuthorizationGeneration($config);
        return $generation->generate();
    }


    /**
     * 生成基础模型基类
     * @param $modulePath
     * @return mixed
     * @throws Exception
     */
    public function generationBaseModel($modulePath)
    {
        $config = new ModelConfig("BaseModel", "App\\Model{$modulePath}");
        $config->setExtendClass(AbstractModel::class);
        $generation = new ModelGeneration($config);
        return $generation->generate();
    }

    /**
     * 生成异常类
     * @return array
     * @throws Exception
     */
    public function generationException(): array
    {
        $result = [];
        $exceptionClassArray = ['AuthorizationException'];
        foreach ($exceptionClassArray as $class) {
            $config = new Config($class, "App\\Exception");
            $config->setExtendClass(Exception::class);
            $generation = new ExceptionGeneration($config);
            $result[$class] = $generation->generate();
        }
        return $result;

    }


    /**
     * 生成单元测试基类
     * @param $modulePath
     * @return bool|int
     * @throws Exception
     */
    public function generationBaseUnitTest($modulePath)
    {
        $config = new UnitTestConfig("BaseTest", "UnitTest{$modulePath}");
        $config->setExtendClass(TestCase::class);
        $generation = new UnitTestGeneration($config);
        return $generation->generate();
    }

}